package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
//    key=&page=1&rows=5&sortBy=name&desc=false

    @Autowired
    private BrandService brandService;
    @GetMapping(value = "/page",name = "分页查询品牌数据")
    public ResponseEntity<PageResult<BrandDTO>> findByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows ,
            @RequestParam(value = "rows",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc){
        PageResult<BrandDTO> pageResult =  brandService.findByPage(key,page,rows,sortBy,desc);
      return ResponseEntity.ok(pageResult);
    }

    @PostMapping(name = "新增品牌")
    public ResponseEntity<Void> save(BrandDTO brandDTO,@RequestParam("cids") List<Long> cids){
        brandService.save(brandDTO,cids);
//        return ResponseEntity.ok().build(); 200
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping(name = "修改品牌")
    public ResponseEntity<Void> update(BrandDTO brandDTO,@RequestParam("cids") List<Long> cids){
        brandService.update(brandDTO,cids);
//        return ResponseEntity.ok().build(); 200
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping(value = "/of/category",name = "根据分类id查询品牌数据")
    public ResponseEntity<List<BrandDTO>> findBrandByCategoryId(@RequestParam("id") Long id){
        List<BrandDTO> brandDTOList = brandService.findBrandByCategoryId(id);
//        return ResponseEntity.ok().build(); 200
        return ResponseEntity.ok(brandDTOList);
    }

    @GetMapping(value = "/list",name = "根据品牌id集合查询品牌集合数据")
    public ResponseEntity<List<BrandDTO>> findBrandByIds(@RequestParam("ids") List<Long> ids){
        List<BrandDTO> brandDTOList = brandService.findBrandByIds(ids);
//        return ResponseEntity.ok().build(); 200
        return ResponseEntity.ok(brandDTOList);
    }

    @GetMapping(value = "/{id}",name = "根据品牌id查询品牌")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id")  Long  id ){
        BrandDTO brandDTO = brandService.findBrandById(id);
//        return ResponseEntity.ok().build(); 200
        return ResponseEntity.ok(brandDTO);
    }

}
